package com.baox.agridata.synchronous.util;

import com.baox.agridata.synchronous.result.HttpResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.json.XML;

import java.net.URLEncoder;

@Slf4j
public class MessageUtil {

    public static String BASEURL = "http://sms.800617.com:4400/sms/SendSMS.aspx";
    public static String CONTENT = "【四川智慧动监】温馨提示：尊敬的承运人，系统已接收到您所承运动物的检疫信息。请您根据运输路线就近选择入川动物运输指定通道接受监督检查并获取签章。对发现未经指定通道入川的，农业农村部门将依法追究责任。您可搜索《关于设立入川动物运输指定通道的通告》或关注“四川智慧动监”微信公众号查询检查站位置信息，也可通过高德地图软件搜索检查站或点击链接【https://bit.ly/48nAMSn】导航前往。";
    public static String CONTENT1 = "【智慧动监】手机号：%s请求的验证码为：%s，请于15分钟内填写。为了保障信息安全，请勿将验证码告诉他人。如非本人操作，请忽略本短信！";

    public static String ACCOUNTNAME = "bjbxsy-1";
    public static String ACCOUNTPASSWORD = "4cef27";
    private static final String CODE_FORMAT = "gb2312";


    public static Integer sendMessage(String phone) {
        String smsContent = URLEncoder.encode(CONTENT);
        StringBuilder url = new StringBuilder();
        url.append(BASEURL).append("/?");
        url.append("un=").append(ACCOUNTNAME).append("&");
        url.append("pwd=").append(ACCOUNTPASSWORD).append("&");
        url.append("mobile=").append(phone).append("&");
        url.append("msg=").append(smsContent);
        HttpResultResponse resultResponse = HttpUtils.get(url.toString());
        JSONObject result = XML.toJSONObject(resultResponse.getBody());
        log.info("发送短信返回结果是：{}", result.toString());
        System.out.println(result.toString());
        JSONObject rootJsonObj = result.getJSONObject("Root");
        Integer sendResult = rootJsonObj.getInt("Result");
        if (sendResult != null && sendResult.intValue() == 1) {
            return sendResult.intValue();
        }
        return 0;
    }


    public static Integer sendMessageOther(String sendPhone,String code,String checkPhone) {
        String content = String.format(CONTENT1,checkPhone,code);
        String smsContent = URLEncoder.encode(content);
        StringBuilder url = new StringBuilder();
        url.append(BASEURL).append("/?");
        url.append("un=").append(ACCOUNTNAME).append("&");
        url.append("pwd=").append(ACCOUNTPASSWORD).append("&");
        url.append("mobile=").append(sendPhone).append("&");
        url.append("msg=").append(smsContent);
        HttpResultResponse resultResponse = HttpUtils.get(url.toString());
        JSONObject result = XML.toJSONObject(resultResponse.getBody());
        log.info("发送短信返回结果是：{}", result.toString());
        System.out.println(result.toString());
        JSONObject rootJsonObj = result.getJSONObject("Root");
        Integer sendResult = rootJsonObj.getInt("Result");
        if (sendResult != null && sendResult.intValue() == 1) {
            return sendResult.intValue();
        }
        return 0;
    }

}
